package de.placidi.pressreview.textprocessing;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import de.placidi.pressreview.parsing.CountedWord;

/**
 * Contains information about the text. How often a word occurs and which neighbors it has. 
 * 
 * @author Alexander Placidi
 *
 */

public class StatisticWords {
	
	private List<CountedWord> cWords;
	private HashMap<String, AdjacentWords> adjMap = new HashMap<>();
	private Integer maxFrequency = new Integer(0);
	
	public List<CountedWord> getcWords() {
		return cWords;
	}
	
	public void setcWords(List<CountedWord> cWords) {
		this.cWords = cWords;
	}
	
	public HashMap<String, AdjacentWords> getAdjMap() {
		return adjMap;
	}
	
	public void setAdjMap(HashMap<String, AdjacentWords> adjMap) {
		this.adjMap = adjMap;
	}
	
	/**
	 * Provides a list of the most frequent words. 
	 * 
	 * @param top
	 * 		how many top words you want
	 * @return
	 */
	public List<CountedWord> getTopWords(Integer top) {
		List<CountedWord> topWords = new ArrayList<>();
		
		Collections.sort(cWords);
		for (int i=0; i < cWords.size(); i++) {
			if (i < top) {
				topWords.add(new CountedWord(cWords.get(i).getWord(), cWords.get(i).getFrequency()));
			} else {
				break;
			}
		}
		
		return topWords;
	}
	
	/**
	 * Provides a list of the most frequent words. Scales the frequency to the given value.
	 * 
	 * @param top
	 * 		how many top words you want
	 * @param scale
	 * 		the scale
	 * @return
	 */
	public List<CountedWord> getTopWords(Integer top, Integer scale) {
		List<CountedWord> topWords = new ArrayList<>();
		
		Collections.sort(cWords);
		for (int i=0; i < cWords.size(); i++) {
			if (i < top) {
				Integer strength = computeStrength(cWords.get(i).getFrequency(), scale);
				topWords.add(new CountedWord(cWords.get(i).getWord(), strength));
			} else {
				break;
			}
		}
		
		return topWords;
	}
	
	private Integer computeStrength(Integer frequency, Integer scale) {
		Integer strength = new Integer(0);
		
		strength = (int) ((frequency.floatValue()/maxFrequency.floatValue())*scale);  
		
		return strength;
	}
	
	public Integer getMaxFrequency() {
		return maxFrequency;
	}
	
	public void setMaxFrequency(Integer maxFrequency) {
		this.maxFrequency = maxFrequency;
	}
	
	
}
